package com.jumi.microservice.controller.phpApi;

import com.jumi.microservice.common.base.dto.ResponseResult;
import com.jumi.microservice.common.utils.ResultGenerator;
import com.jumi.microservice.constant.ExceptionEnum;
import com.jumi.microservice.domain.MessageTemplate;
import com.jumi.microservice.domain.vo.MessageTemplateVO;
import com.jumi.microservice.service.IMessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;
@RestController
@Api(tags = "PHP所需接口")
@RequestMapping("/phpApi/message")
public class MessageTemplatePHPController {
    @Resource
    private IMessageService iMessageService;


    @ApiOperation(value = "获取消息模板信息", notes = "获取消息模板信息")
    @GetMapping("/messageTemplateList")
    public ResponseResult<List<MessageTemplateVO>> messageTemplateList(){
        List<MessageTemplate> messageTemplates = iMessageService.messageTemplateList();
        return ResultGenerator.genSuccessResult(pos2VosTemplate(messageTemplates));
    }


    @ApiOperation(value = "获取单个消息模板信息", notes = "获取单个消息模板信息")
    @ApiImplicitParam(name = "TemplateId", value = "消息id")
    @GetMapping("/messageTemplateOne")
    public ResponseResult<MessageTemplateVO> messageTemplateOne(Long TemplateId){
        MessageTemplate messageTemplates = iMessageService.messageTemplateOne(TemplateId);
        return ResultGenerator.genSuccessResult(dto2VoTemplate(messageTemplates));
    }

    private List<MessageTemplateVO> pos2VosTemplate(List<MessageTemplate> messageTemplates) {
        List<MessageTemplateVO> pos = messageTemplates.stream().map(p -> {
            MessageTemplateVO vo = null;
            try {
                vo = p.clone(MessageTemplateVO.class);
            } catch (Exception e) {
                ExceptionEnum.FAIL_CHANGE.newException();
            }
            return vo;
        }).collect(Collectors.toList());
        return pos;
    }

    private MessageTemplateVO dto2VoTemplate(MessageTemplate dto) {
        MessageTemplateVO vo = null;
        try {
            vo = dto.clone(MessageTemplateVO.class);
        } catch (Exception e) {
            ExceptionEnum.FAIL_CHANGE.newException();
        }
        return vo;
    }
}
